package org.example.volunteerend.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.example.volunteerend.common.Result;
import org.example.volunteerend.dto.ServiceRecordRequest;
import org.example.volunteerend.entity.ServiceRecord;
import org.example.volunteerend.entity.User;
import org.example.volunteerend.mapper.UserMapper;
import org.example.volunteerend.service.ServiceRecordService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/api/service-records")
@RequiredArgsConstructor
public class ServiceRecordController {

    private final ServiceRecordService serviceRecordService;
    private final UserMapper userMapper;
    
    /**
     * 添加服务记录
     */
    @PostMapping
    public Result<Map<String, Object>> addServiceRecord(@RequestBody ServiceRecordRequest request, HttpServletRequest httpRequest) {
        // 从token中获取用户ID（可选）
        Integer tokenUserId = (Integer) httpRequest.getAttribute("userId");
        
        try {
            // 添加服务记录
            Integer recordId = serviceRecordService.addServiceRecord(request);
            
            // 构建返回结果
            Map<String, Object> data = new HashMap<>();
            data.put("record_id", recordId);
            
            return Result.success("服务记录添加成功", data);
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务记录添加失败：" + e.getMessage());
        }
    }
    
    /**
     * 上传服务证明图片
     */
    @PostMapping("/proof-images")
    public Result<Map<String, Object>> uploadProofImages(@RequestParam("images") List<MultipartFile> imageFiles) {
        try {
            // 上传服务证明图片
            List<String> imageUrls = serviceRecordService.uploadProofImages(imageFiles);
            
            // 构建返回结果
            Map<String, Object> data = new HashMap<>();
            data.put("image_urls", imageUrls);
            
            return Result.success("图片上传成功", data);
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("图片上传失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取服务记录详情
     */
    @GetMapping("/{recordId}")
    public Result<Map<String, Object>> getServiceRecordDetail(@PathVariable Integer recordId) {
        try {
            // 获取服务记录详情
            ServiceRecord record = serviceRecordService.getServiceRecordDetail(recordId);
            
            // 获取用户信息
            User user = userMapper.findById(record.getUserId());
            
            // 构建返回结果
            Map<String, Object> data = new HashMap<>();
            data.put("record_id", record.getRecordId());
            data.put("user_id", record.getUserId());
            data.put("username", user != null ? user.getUsername() : null);
            data.put("real_name", user != null ? user.getRealName() : null);
            data.put("project_id", record.getProjectId());
            data.put("project_name", record.getProjectName());
            data.put("service_date", record.getServiceDate());
            data.put("service_hours", record.getServiceHours());
            data.put("description", record.getDescription());
            data.put("proof_images", record.getProofImages());
            
            return Result.success(data);
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取服务记录详情失败：" + e.getMessage());
        }
    }
} 